﻿using DBO_ShopMob;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WS_Manager_ShopMob.Helper;

namespace WS_Manager_ShopMob.Model
{
    /// <summary>
    /// Repository de debug avec des données en dur
    /// </summary>
    class DebugRepository : IRepository
    {
        /// <summary>
        /// Liste des clients
        /// </summary>
        private List<Client> clients = new List<Client>();

        /// <summary>
        /// Liste des articles
        /// </summary>
        private List<Article> listArticles = new List<Article>();

        /// <summary>
        /// Liste des magasins
        /// </summary>
        private List<Magasin> listMagasin = new List<Magasin>();

        /// <summary>
        /// stocks d'articles avec la quantité pour un magasin
        /// </summary>
        private Dictionary<long, int> stocks = new Dictionary<long, int>();

        /// <summary>
        /// Liste des commandes
        /// </summary>
        private List<Commande> listCommandes = new List<Commande>();

        /// <summary>
        /// Liste des livreurs
        /// </summary>
        private List<DBO_ShopMob.Livreur> listLivreurs = new List<DBO_ShopMob.Livreur>();

        /// <summary>
        /// Construteur standard : on instancie quelques clients pour le debug
        /// </summary>
        public DebugRepository() {
            clients.Add(new Client("qp", "M.", "LEPETIT", "Quentin", "0000", "qp", "", "", "Baudre", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            clients.Add(new Client("ft", "M.", "TAVILLA", "Fabrice", "0000", "ft", "", "95000", "Cergy", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            clients.Add(new Client("sv", "M.", "VALENTIN", "Steve", "0000", "sv", "", "77000", "Melun", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            clients.Add(new Client("kpj", "M.", "PIERRE-JUSTIN", "Karl", "0000", "kpj", "", "93000", "Bobigny", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            clients.Add(new Client("kg", "M.", "GABIN", "Kris", "0000", "kg", "", "91000", "Evry", "0100000000", "0600000000", new DateTime(1990, 4, 5)));
            listLivreurs.Add(new Livreur() { id = 0, idMagasin = 1, email = "sval@hotmail.fr", password = "0" });
            listLivreurs.Add(new Livreur() { id = 1, idMagasin = 1, email = "kpj@hotmail.fr", password = "0" });
            listLivreurs.Add(new Livreur() { id = 2, idMagasin = 2, email = "cgab@hotmail.fr", password = "0" });
            listLivreurs.Add(new Livreur() { id = 3, idMagasin = 4, email = "qle@hotmail.fr", password = "0" });
            listLivreurs.Add(new Livreur() { id = 4, idMagasin = 5, email = "fab@hotmail.fr", password = "0" });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les chaussures", id = 1, nom = "chaussures", prix = 20, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les chaussettes", id = 2, nom = "chaussettes", prix = 20, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Les casques", id = 3, nom = "casques", prix = 20 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "Le ballon", id = 4, nom = "Ballon", prix = 20, categorie = 2 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "La bille", id = 5, nom = "bille", prix = 20});
            listArticles.Add(new DBO_ShopMob.Article() { details = "Le pantalon", id = 6, nom = "pantalon", prix = 20 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "sandwitch grec", id = 7, nom = "kebab", prix = 5.5, categorie = 3 });
            listArticles.Add(new DBO_ShopMob.Article() { details = "classique de mcdonald", id = 8, nom = "big mac", prix = 2, categorie = 3 });
            stocks.Add(1, 15);
            stocks.Add(2, 80);
            stocks.Add(3, 40);
            stocks.Add(4, 26);            
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "1", ville = "Dammartin en goele", stock = stocks, adresse = "", cp = "77230" });
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "2", ville = "paris", stock = stocks, adresse = "26 rue Rivoli", cp = "75002" });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(2, 80);
            stocks.Add(4, 26);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "3", ville = "", stock = stocks, adresse = "", cp = "91000" });
            stocks = new Dictionary<long, int>();
            stocks.Add(6, 10);
            stocks.Add(5, 150);
            stocks.Add(4, 40);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "4", ville = "le raincy", stock = stocks, adresse = "", cp = "93340" });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(2, 80);
            stocks.Add(4, 26);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "5", ville = "cergy", stock = stocks, adresse = "", cp = "95000" });
            stocks = new Dictionary<long, int>();
            stocks.Add(5, 25);
            stocks.Add(1, 10);
            stocks.Add(3, 5);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "6", ville = "marseille", stock = stocks, adresse = "", cp = "94000" });
            stocks = new Dictionary<long, int>();
            stocks.Add(3, 25);
            stocks.Add(2, 180);
            stocks.Add(6, 206);
            listMagasin.Add(new DBO_ShopMob.Magasin() { idMagasin = "7", ville = "cherbourg", stock = stocks, adresse = "", cp = "50100" });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[0], Client = clients[0], Magasin = listMagasin[0], RefCommande = "2", DateCommande = DateTime.Now, Statut = "En attente" });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[1], Client = clients[0], Magasin = listMagasin[2], RefCommande = "1", DateCommande = DateTime.Now.AddDays(-1), Statut = "En attente" });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[0], Client = clients[1], Magasin = listMagasin[0], RefCommande = "5", DateCommande = DateTime.Now.AddDays(-5), Statut = "En cours" });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[2], Client = clients[3], Magasin = listMagasin[1], RefCommande = "6", DateCommande = DateTime.Now.AddDays(-1), Statut = "Terminer", DateLivraison = DateTime.Now.AddDays(1) });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[0], Client = clients[2], Magasin = listMagasin[5], RefCommande = "3", DateCommande = DateTime.Now, Statut = "En attente" });
            listCommandes.Add(new Commande() { Livreur = listLivreurs[2], Client = clients[3], Magasin = listMagasin[4], RefCommande = "4", DateCommande = DateTime.Now.AddDays(-4), Statut = "Terminer", DateLivraison = DateTime.Now.AddDays(3) });
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void Connexion(string login, string motdePasse, ResultHandler methodToExec)
        {
            //on parcours la liste des clients
            methodToExec(JsonConvert.SerializeObject(listLivreurs.SingleOrDefault(x => x.email == login)), null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetClientParID(string login, ResultHandler methodToExec)
        {
            //on parcours la liste des clients
            foreach (Client c in clients)
            {
                if (c.Email == login) {
                    //si on parviens à identifier l'utilisateur on retourne l'objet client sous forme JSON
                    methodToExec(JsonConvert.SerializeObject(c), null);
                    return;
                }
            }
            //sinon on retourne rien
            methodToExec("", null);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetMagasinForArticle(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listMagasin.Where(x => x.stock.Keys.Contains<long>(id))), null);
        }


        public void GetCommandeParId(string id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCommandes.SingleOrDefault(x => x.RefCommande == id)), null);
        }

        public void GetCommandesEnAttente(ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCommandes.Where(x => x.Statut == "En attente").ToList()), null);
        }

        public void GetCommandesEnCours(ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCommandes.Where(x => x.Statut == "En cours").ToList()), null);
        }

        public void GetCommandesTermine(ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCommandes.Where(x => x.Statut == "Termine").ToList()), null);
        }

        public void GetCommandesParLivreur(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listCommandes.Where(x => x.Livreur.id == id).ToList()), null);
        }

        public void GetLivreurs(ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listLivreurs.ToList()), null);
        }

        public void GetLivreurs(long id, ResultHandler methodToExec)
        {
            methodToExec(JsonConvert.SerializeObject(listLivreurs.SingleOrDefault(x => x.id == id)), null);
        }
    }
}
